<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:p="http://primefaces.org/ui"
     xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"
     xmlns:tp="http://ifeed.vgregion.com/facelets" xmlns:c="http://java.sun.com/jsp/jstl/core">

    <h:panelGroup id="organizations" styleClass="organization-tree">
        <table>
            <thead>
            <tr>
                <td colspan="20">
                    Organisationsdel #{(app.newFilter.value != '') ? '' : 'app.newFilter.value'}
                    <h:panelGroup rendered="#{not empty app.newFilter.value}">
                        (Valt värde: #{(app.newFilter.value)})
                    </h:panelGroup>
                </td>
                <td>Vald</td>
            </tr>
            </thead>
            <tbody>

            <ui:repeat value="#{vgrOrganizationsHome.organizations}" var="organization"
                       id="selectVgrOrganizationTree" varStatus="st">
                <tr id="#{organization.getHsaIdentity()}">
                    <h:panelGroup rendered="#{organization.level > 0}">
                        <td colspan="#{(organization.level)}" class="indention"
                            style="width: #{(organization.level + 1)*4}%; border: none; white-space: nowrap;">
                        </td>
                    </h:panelGroup>
                    <td class="toggle-expanded" align="center">
                        <h:commandLink title="Öppna"
                                       rendered="#{not organization.leaf and not organization.open}"
                                       action="#{vgrOrganizationsHome.loadChildrenFlat(app, organization)}">
                            <f:ajax render="@form" action="click"/>
                            <!--img class="toggle-symbol minimized" src="#{request.contextPath}/img/white-cross.png" -->

                            <div class="icon-chevron-right"></div>
                        </h:commandLink>

                        <h:commandLink title="Stäng"
                                       rendered="#{not organization.leaf and organization.open}"
                                       actionListener="#{vgrOrganizationsHome.closeChildrenFlat(organization)}">
                            <f:ajax render="@form" action="click"/>
                            <div class="icon-chevron-down"></div>
                            <!--img class="toggle-symbol expanded" src="#{request.contextPath}/img/white-cross.png" -->
                        </h:commandLink>
                    </td>
                    <td colspan="#{20 - (organization.level + 1)}" title="#{organization.getHsaIdentity()}">
                        #{organization.getLabel()}
                    </td>

                    <td>
                        <h:commandLink title="Välj"
                                       rendered="#{vgrOrganizationsHome.indexOfOrgInFeed(app, organization) == -1}"
                                       actionListener="#{vgrOrganizationsHome.toggleOrganizationCondition(app, organization)}">
                            <input type="checkbox"/>
                        </h:commandLink>
                        <h:commandLink title="Välj"
                                       rendered="#{vgrOrganizationsHome.indexOfOrgInFeed(app, organization) != -1}"
                                       actionListener="#{vgrOrganizationsHome.toggleOrganizationCondition(app, organization)}">
                            <input type="checkbox" checked="checked"/>
                        </h:commandLink>
                    </td>
                </tr>
            </ui:repeat>
            </tbody>
        </table>
        <h:outputScript>
            function on() {
            var lastlyPushed = document.getElementById('#{vgrOrganizationsHome.lastHsaIdClicked}');
            if (lastlyPushed){
            //console.log("Hittade", '#{vgrOrganizationsHome.lastHsaIdClicked}');
            lastlyPushed.scrollIntoView({block: "end", behavior: "smooth"});
            } else {
            //console.log("Hittade inte ", '#{vgrOrganizationsHome.lastHsaIdClicked}');
            }
            refreshResults();
            }
            setTimeout(on, 100);
        </h:outputScript>
    </h:panelGroup>

</div>